<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleAdd">新增</a-button>
        <Popconfirm
          title="确认删除选中的账号吗?"
          ok-text="确认"
          cancel-text="取消"
          placement="topRight"
          @confirm="handleDeleteMany"
        >
          <a-button type="primary">批量删除</a-button>
        </Popconfirm>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '编辑',
              onClick: handleEdit.bind(null, record),
              color: 'success',
            },
            {
              disabled: record.status === '已下线' ? true : false,
              label: '推荐到首页',
              color: 'warning',
            },
            {
              label: record.status === '已上线' ? '下线' : '上线',
              onClick: handleChangeStatus.bind(null, record),
            },
            {
              label: '删除',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <UserModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>

<script lang="ts">
  import { BasicTable, TableAction, useTable } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { defineComponent } from 'vue';
  import UserModal from './propertyInfoModal.vue';
  import { columns, dataSource, searchFormSchema } from './propertyInfoManage.data';
  import { message, Popconfirm } from 'ant-design-vue';

  export default defineComponent({
    components: {
      BasicTable,
      UserModal,
      TableAction,
      Popconfirm,
    },
    setup() {
      // 新增或编辑对话框
      const [registerModal, { openModal }] = useModal();

      const [registerTable, { reload, getSelectRowKeys, clearSelectedRowKeys }] = useTable({
        // api: queryList,
        dataSource,
        useSearchForm: true,
        formConfig: {
          labelWidth: 80,
          schemas: searchFormSchema,
          actionColOptions: { span: 4 },
        },
        columns: columns,
        clickToRowSelect: false,
        rowSelection: { type: 'checkbox' },
      });
      // 编辑
      const handleEdit = (record) => {
        console.log(record);
        // 获取编辑行的数据
        // *
        openModal(true, {
          isUpdate: true,
          record: JSON.parse(JSON.stringify(record)),
        });
      };
      // 修改状态
      const handleChangeStatus = (record) => {
        record.status = record.status === '已上线' ? '已下线' : '已上线';
      };
      // 单个删除
      const handleDelete = (record) => {
        console.log(record);
      };
      // 多个删除
      const handleDeleteMany = () => {
        const deleteArr = getSelectRowKeys();
        if (deleteArr && deleteArr.length === 0) {
          return message.warn('请选择要删除的数据！');
        } else {
          message.success('删除成功');
          clearSelectedRowKeys();
        }
      };
      // 新增
      const handleAdd = () => {
        openModal(true, {
          isUpdate: false,
        });
      };
      // 新增成功
      const handleSuccess = () => {
        reload();
      };
      return {
        registerTable,
        registerModal,
        handleEdit,
        handleChangeStatus,
        handleDelete,
        handleAdd,
        handleSuccess,
        handleDeleteMany,
      };
    },
  });
</script>

<style scoped lang="less"></style>
